import React from "react";
import {Navigate, useRoutes} from "react-router-dom";
import LoginPage from "@/pages/login";
import {useSelector} from "@/redux/hooks";
import LayoutPage from "@/pages/layout";
import {DashboardPage} from "@/pages/dashboard";
import {SoftwarePage} from "@/pages/software";
import {ScriptPage} from "@/pages/script";
import {TaskPage} from "@/pages/task";
import {DockerContainer} from "@/pages/docker/container";
import {DockerImages} from "@/pages/docker/images";
import {DockerNetwork} from "@/pages/docker/network";
import {DockerVolumes} from "@/pages/docker/volumes";
import {DockerPage} from "@/pages/docker";
import {SoftwareStore} from "@/pages/software/store";
import {SoftwareInstall} from "@/pages/software/install";
import {ToolsPage} from "@/pages/tools";
import {ToolsFilePage} from "@/pages/tools/file";
import {ToolsTerminusPage} from "@/pages/tools/terminus";
import {ToolsVisitPage} from "@/pages/tools/visit";
import {PersonalPage} from "@/pages/personal";
import AuthorityPage from "@/pages/authority";
import AuthorityUserPage from "@/pages/authority/user";
import AuthorityRolePage from "@/pages/authority/role";
import AuthorityPermissionPage from "@/pages/authority/permission";
import NotAuthPage from "@/pages/exception/403";
import NotFoundPage from "@/pages/exception/404";
import ServerErrorPage from "@/pages/exception/500";

export const SystemRouter = (): React.ReactElement | null => {
	const user = useSelector((state) => state.user)

	return useRoutes(!user.status ? [
		{
			path: '*',
			element: <Navigate to={'/login'} />
		},
		{
			path: 'login',
			element: <LoginPage />
		},
	]: [
		{
			path: '*',
			element: <Navigate to={'/dashboard'} />
		},
		{
			path: 'login',
			element: <LoginPage />
		},
		{
			path: '',
			element: <LayoutPage />,
			children: [
				{
					path: 'dashboard',
					element: <DashboardPage />
				},
				{
					path: 'software',
					element: <SoftwarePage />,
					children: [
						{
							path: 'store',
							element: <SoftwareStore />
						},
						{
							path: 'install',
							element: <SoftwareInstall />
						}
					]
				},
				{
					path: 'docker',
					element: <DockerPage />,
					// element: <Navigate to={"/docker/container"} />,
					children: [
						{
							path: 'container',
							element: <DockerContainer />
						},
						{
							path: 'images',
							element: <DockerImages />
						},
						{
							path: 'network',
							element: <DockerNetwork />
						},
						{
							path: 'volumes',
							element: <DockerVolumes />
						},
					]
				},
				{
					path: 'tools',
					element: <ToolsPage />,
					children: [
						{
							path: 'file',
							element: <ToolsFilePage />
						},
						{
							path: 'terminus',
							element: <ToolsTerminusPage />
						},
						{
							path: 'visit',
							element: <ToolsVisitPage />
						}
					]
				},
				{
					path: 'script',
					element: <ScriptPage />
				},
				{
					path: 'task',
					element: <TaskPage />
				},
				{
					path: 'authority',
					element: <AuthorityPage />,
					children: [
						{
							path: 'user',
							element: <AuthorityUserPage />,
						},
						{
							path: 'role',
							element: <AuthorityRolePage />,
						},
						{
							path: 'permission',
							element: <AuthorityPermissionPage />,
						}
					]
				},
				{
					path: 'person',
					element: <PersonalPage />
				},
				{
					path: '403',
					element: <NotAuthPage />
				},
				{
					path: '404',
					element: <NotFoundPage />
				},
				{
					path: '500',
					element: <ServerErrorPage />
				}
			]
		},

	])
}
